package com.mew.slots.core.game.fkymr.room;

import cn.hutool.core.util.RandomUtil;
import com.mew.slots.core.common.game.BaseRoom;
import com.mew.slots.core.game.fkymr.dto.FkymrInRoom;
import com.mew.slots.core.game.fkymr.dto.FkymrPlayMsgs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class FkymrRoom extends BaseRoom {

    public static int _SCATTER = 12;//_WILD不允许替换

    public static int _FREE = 11;

    private FkymrInRoom fkymrInRoom;

    public FkymrRoom(FkymrInRoom fkymrInRoom){
        this.fkymrInRoom=fkymrInRoom;
        res = new FkymrPlayMsgs();
        row = 3;
        col = 5;
        _WILD = 10;
        spes = new ArrayList<Integer>(){{
            add(11);
            add(12);
        }};

         rate = new int[][]{
                 {0,  0,  5,  15,  50},//锅                  0
                 {0,  0,  5,  15,  50},//炸药桶               1
                 {0,  0,  5,  15,  50},//茅草房               2
                 {0,  0,  5,  15,  50},//木房                 3
                 {0,  0,  5,  15,  50},//砖瓦房               4
                 {0,  0, 10,  25, 100},//狗                  5
                 {0,  0, 10,  30, 150},//红猪                 6
                 {0,  0, 10,  30, 150},//绿猪                 7
                 {0,  0, 10,  30, 150},//蓝猪                 8
                 {0,  5, 15,  50, 300},//猪妈妈               9
                 {0,  5, 15,  50, 300},//_WILD               10
                 {0,  0,  5,  10,  25},//FREE 金币猪          11
                 {0,  0,  0,   0,   0},//_SCATTER 转盘        12
        };

         alllines = new int[][]{
                 {5,6,7,8,9},
                 {0,1,2,3,4},
                 {10,11,12,13,14},
                 {0,6,12,8,4},
                 {10,6,2,8,14},
                 {5,1,2,3,9},
                 {5,11,12,13,9},
                 {0,1,7,13,14},
                 {10,11,7,3,4},
                 {5,11,7,3,9},
                 {5,1,7,13,9},
                 {0,6,7,8,4},
                 {10,6,7,8,14},
                 {0,6,2,8,4},
                 {10,6,12,8,14},
                 {5,6,2,8,9},
                 {5,6,12,8,9},
                 {0,1,12,3,4},
                 {10,11,2,13,14},
                 {0,11,12,13,4},
                 {10,1,2,3,14},
                 {5,11,2,13,9},
                 {5,1,12,3,9},
                 {0,11,2,13,4},
                 {10,1,12,3,14},
                 {10,1,7,13,4},
                 {0,11,7,3,14},
                 {0,11,7,13,4},
                 {10,1,7,3,14},
                 {10,6,2,3,9},
                 {0,6,12,13,9},
                 {0,1,12,13,14},
                 {10,11,2,3,4},
                 {5,1,12,8,14},
                 {5,11,2,8,4},
                 {0,6,2,8,14},
                 {10,6,12,8,4},
                 {5,11,12,3,4},
                 {0,1,7,8,14},
                 {10,11,7,8,4},
                 {10,1,2,3,4},
                 {0,11,12,13,14},
                 {10,11,12,13,4},
                 {0,1,2,3,14},
                 {5,1,7,3,9},
                 {5,11,7,13,9},
                 {0,6,12,13,14},
                 {10,6,2,3,4},
                 {0,6,7,8,9},
                 {10,6,7,8,9}
        };
    }
    @Override
    public void getLinesXml(){

        if (controlType>0){
            endSlots = control();
        } else {
            endSlots = rollerXml.getLineInts(free?1:0,rollerID);
        }
        if(free){
            replaceRemoveIcon();
        }
        res.endSlots=endSlots.clone();
    }
    @Override
    public void getLinesIli(){
        int[] rs;
        if (controlType>0){
            rs = control();
        } else {
            rs = getIliEndSlots();
        }
        endSlots = Arrays.copyOf(rs,col*row);
        FkymrPlayMsgs pmRes= (FkymrPlayMsgs) res;
        for (int i = col*row; i < rs.length; i++) {
            if(rs[i]!=-1){
                pmRes.getRemoveIcon().add(rs[i]);
            }

        }
  /*      if(free){
            replaceRemoveIcon();
        }*/
        res.endSlots=endSlots.clone();
    }


    private void replaceRemoveIcon(){
        int temp=0;
        for (int i = 4; i >= 0; i--) {
            if(!fkymrInRoom.getRemoveIcon().contains(i)){
                temp=i;
            }
        }
        for (int i = 0; i < endSlots.length; i++) {
            if(fkymrInRoom.getRemoveIcon().contains(endSlots[i])){
                endSlots[i]=temp;
            }
        }
    }

    @Override
    public void freeBouns(){
        resolverFree();
        computeBonus();
    }
    private void resolverFree(){
        FkymrPlayMsgs pmres=(FkymrPlayMsgs) res;
        int freeNum=fsdb(_FREE);
        freeNum=freeNum>5?5:freeNum;
        if(freeNum>2){
/*            if(!free) {
                List<Integer> removeIcon = computeRemoveIcon();
                fkymrInRoom.setRemoveIcon(removeIcon);
                pmres.setRemoveIcon(removeIcon);
            }*/

            pmres.setFree((byte)1);
            pmres.setFreeNumber(freeNum==3?7:freeNum==4?9:13);
            pmres.setMultiple(res.getMultiple()+rate[_FREE][freeNum-1]);
        }

    }
    private List<Integer> computeRemoveIcon() {
        ArrayList<Integer> tempNum = new ArrayList<>();
        int dri = RandomUtil.randomInt(0, 5);
        for (int i = 0; i < 5; i++) {
            if (dri != i) {
                tempNum.add(i);
            }
        }
        Collections.shuffle(tempNum);
        return tempNum;
    }
    private void computeBonus(){
        int snum=fsdb(_SCATTER);
        if(snum>2){
            res.setBouns(1);
        }
    }
   /* @Override
    public int[] control(){
        Controlline controlline=new Controlline();
        switch (controlType) {
            case 1:
                return bigwin();
            case 2:
                return controlline.free();
            case 3:
                return controlline.bonus();
            default:
                return controlline.empty();
        }
    }*/

    @Override
    public int[] control(){
        switch (controlType) {
            case 1:
                return  iliXml.getSpeints("21-23",in);
            case 2:
                return  iliXml.getSpeints("free",in);
            case 3:
                return  iliXml.getSpeints("bonus",in);
            default:
                return  iliXml.getSpeints("0-0",in);
        }
    }
}
